package com.wan.data.resource.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wwh
 * @date 2022/7/5 15:59
 */
public class NormalCache<ID,M extends IModel<ID>> implements ModelCache<ID,M>{

    private Map<ID,M> modelCache = new HashMap<>();

    @Override
    public M get(ID id) {
        return modelCache.get(id);
    }

    @Override
    public List<M> getAll() {
        return new ArrayList<>(modelCache.values());
    }

    @Override
    public void init(List<M> data) {
        modelCache = data.stream().collect(Collectors.toMap(M::getID,model->model));
    }
}
